package com.nit.bike.controller.bike;

import com.alibaba.fastjson.JSONObject;
import com.nit.bike.dto.bike.CardRecordDTO;
import com.nit.bike.dto.system.TokenDTO;
import com.nit.bike.dto.utils.OpResultDTO;
import com.nit.bike.dto.utils.TableReqDTO;
import com.nit.bike.dto.utils.TableRspDTO;
import com.nit.bike.service.bike.CardRecordService;
import com.nit.bike.utils.AuthorizationUtil;
import com.nit.bike.utils.HttpRequestReader;
import com.nit.bike.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/cardRecord")
public class CardRecordController {
    @Resource
    private CardRecordService cardRecordService;
    private final Logger logger = LoggerFactory.getLogger(CardRecordController.class);

    /**
     * 获取数据列表
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO req) {
        TableRspDTO rsp = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (tokenDTO.getRoleId() == 1) {
                String userId = req.parseQueryParam("userId");
                if (userId != null) {
                    rsp = cardRecordService.list4Table(req, Long.parseLong(userId));
                } else {
                    rsp = cardRecordService.list4Table(req, tokenDTO.getAccountId());
                }
            } else {
                rsp = cardRecordService.list4Table(req, tokenDTO.getAccountId());
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return rsp;
    }

    /**
     * 车辆使用记录
     */
    @RequestMapping(value = "/query4Bike", method = RequestMethod.POST)
    public TableRspDTO list4Bike(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO req) {
        TableRspDTO rsp = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (tokenDTO.getRoleId() == 1) {
                rsp = cardRecordService.list4Bike(req);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return rsp;
    }

    /**
     * 新增记录
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-Token") String token,
                           @RequestBody CardRecordDTO cardRecordDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                cardRecordService.add(cardRecordDTO);
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 更新记录
     * 非特殊情况不建议使用
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestHeader(name = "Access-Token") String token,
                              @RequestBody CardRecordDTO cardRecordDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                op.setResult(cardRecordService.update(cardRecordDTO).toString());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 删除记录
     * 非特殊情况不建议使用
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(@RequestHeader(name = "Access-Token") String token,
                              HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
                String stationIds = jsonRequest.getString("ids");
                op.setMessage(cardRecordService.remove(stationIds).toString());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }
}
